package com.joker.aigc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joker.aigc.domain.entity.UserRole;
import com.joker.aigc.domain.enums.RoleEnum;
import com.joker.aigc.service.UserRoleService;
import com.joker.aigc.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 20346
 * @description 针对表【user_role(角色表)】的数据库操作Service实现
 * @createDate 2023-12-17 16:29:00
 */
@Service
@Transactional
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {


    @Override
    public boolean hasPower(Long uid, RoleEnum roleEnum) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(uid != null, UserRole::getUid, uid)
                .eq(roleEnum != null, UserRole::getRoleType, roleEnum.getId());
        UserRole userRole = queryByUid(uid, null);
        // 如果用户信息是否保护
        return userRole == null;
    }

    @Override
    public UserRole queryByUid(Long uid, Long roomId) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(uid != null, UserRole::getUid, uid)
                .eq(roomId != null, UserRole::getRoomId, roomId);
        return getOne(wrapper);
    }

}




